package com.hmall.controller;

import com.hmall.common.dto.Item;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.RequestParams;
import com.hmall.feign.clients.ItemClient;
import com.hmall.pojo.PageResult;
import com.hmall.service.ISearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/search")
public class SearchIndexController {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private ISearchService searchService;

    @PostMapping("/list")
    public PageResult list(@RequestBody RequestParams params) {
        log.info("参:{}", params.toString());
        //- 调用item-service提供的FeignClient，分页查询商品 PageDTO<Item>
        PageDTO<Item> itemDTO = itemClient.itemPageInfo(params.getPage(), params.getSize());

        searchService.bulkDocumentData(itemDTO);        //批量保存文档数据

        PageResult pageResult = searchService.search(params);   //文档数据分页查询
        return pageResult;
    }

    @GetMapping("/suggestion")
    public List<String> suggestion(String key) {
        log.info("分词关键字:[{}]", key);
        return searchService.getSuggestions(key);
    }

    @PostMapping("/filters")
    public Map<String,List<String>> filter(@RequestBody RequestParams params) {
        log.info("参数:{}",params);
        return searchService.filters(params);
    }

}
